package gae.colopl.http.user;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Semaphore;

public class SessionImpl implements Session {
    
    private String username;
    private String password;
    private String id;
    private String uid;
    private String jsessionid;
    private String pt;
    private String hdb;
    private Map<String, Object> attributes = new HashMap<String, Object>();
    private Semaphore semaphore = new Semaphore(1);
    
    
    public SessionImpl(String username,
            String password,
            String id,
            String uid,
            String jsessionid,
            String pt) {
        
        this.username = username;
        this.password = password;
        this.id = id;
        this.uid = uid;
        this.jsessionid = jsessionid;
        this.pt = pt;
    }
    
    public void acquire() throws InterruptedException {
        semaphore.acquire();
    }
    
    public void release() {
        semaphore.release();
    }
    
    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#isLoggedIn()
     */
    @Override
    public boolean isLoggedIn() {
        if (id == null || uid == null || jsessionid == null) {
            return false;
        }
        return true;
    }

    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#getUsername()
     */
    @Override
    public String getUsername() {
        return username;
    }

    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#setUsername(java.lang.String)
     */
    @Override
    public void setUsername(String username) {
        this.username = username;
    }

    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#getPassword()
     */
    @Override
    public String getPassword() {
        return password;
    }

    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#setPassword(java.lang.String)
     */
    @Override
    public void setPassword(String password) {
        this.password = password;
    }

    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#getId()
     */
    @Override
    public String getId() {
        return id;
    }

    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#setId(java.lang.String)
     */
    @Override
    public void setId(String id) {
        this.id = id;
    }

    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#getUid()
     */
    @Override
    public String getUid() {
        return uid;
    }

    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#setUid(java.lang.String)
     */
    @Override
    public void setUid(String uid) {
        this.uid = uid;
    }

    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#getJsessionid()
     */
    @Override
    public String getJsessionid() {
        return jsessionid;
    }

    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#setJsessionid(java.lang.String)
     */
    @Override
    public void setJsessionid(String jsessionid) {
        this.jsessionid = jsessionid;
    }

    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#getPt()
     */
    @Override
    public String getPt() {
        return pt;
    }

    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#setPt(java.lang.String)
     */
    @Override
    public void setPt(String pt) {
        this.pt = pt;
    }

    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#getHdb()
     */
    @Override
    public String getHdb() {
        return hdb;
    }

    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#setHdb(java.lang.String)
     */
    @Override
    public void setHdb(String hdb) {
        this.hdb = hdb;
    }
    
    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#setAttribute(java.lang.String, java.lang.Object)
     */
    @Override
    public void setAttribute(String key, Object value) {
        this.attributes.put(key, value);
    }
    
    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#getAttribute(java.lang.String)
     */
    @Override
    public Object getAttribute(String key) {
        return this.attributes.get(key);
    }

    /* (non-Javadoc)
     * @see gae.colopl.http.user.Session#toString()
     */
    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password
                + ", id=" + id + ", uid=" + uid + ", jsessionid=" + jsessionid
                + ", pt=" + pt + ", hdb=" + hdb + "]";
    }
    

}
